// pages/tucaocomments/tucaocomments.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    id:0,
    tuocao:{},
    title:'',
    imghidden: [],
    comment:''
  },
  subcomment:function(){
    wx.showLoading({
      title: '请稍等',
    })
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/comments', // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'POST',
      data: {
        aid: this.data.tuocao.id,
        content: this.data.comment,
      },
      success: (res) => {
        console.log(res, '成功品论')
        wx.hideLoading();
        if (res.statusCode == 200) {
          app.flag = 1;
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success: () => {
             this.preloading2(this.data.id)
            }
          })
        }
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  submitcomment: function(e){
    // console.log(e.detail.value)
    this.setData({
      comment: e.detail.value
    })
  },
  bindimgloaded:function(e){
    console.log('加载完了')
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    var sss = 'imghidden['+index+']'
    this.setData({
      [sss]:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: app.baseUrl,
      id: options.id
    })
    this.preloading(options.id)
  },

  preloading:function(id){
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/trashes/' + id, // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res, '成功 单个')
        var imghidden = []
        for (var i = 0; i < res.data.images.length; i++) {
          imghidden[i] = true
        }
        
        this.setData({
          tuocao: res.data,
          title: res.data.summary.substring(0, 20),
          imghidden: imghidden
        })
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  preloading2: function (id) {
    var accesstoken = wx.getStorageSync('accesstoken')
    wx.request({
      url: 'https://api.tianlala.com.cn/trashes/' + id, // 仅为示例，并非真实的接口地址
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': accesstoken
      },
      method: 'GET',
      data: {
      },
      success: (res) => {
        console.log(res, '成功 单个')
        // var imghidden = []
        // for (var i = 0; i < res.data.images.length; i++) {
        //   imghidden[i] = true
        // }
        this.setData({
          tuocao: res.data,
          title: res.data.summary.substring(0, 20),
          comment: ''
        })
      },
      fail: (res) => {
        console.log(res, '失败')
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})